package com._21cn.cms.template.utils;

import com._21cn.framework.utils.StringUtil;
import java.io.PrintStream;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PageTurnUtil
{
    public static String[] getPageNav(String currentLink, int totalPage)
    {
        if (totalPage > 1) {
            String prev = "";
            StringBuffer pageMid = new StringBuffer();
            String next = "";
            String preLink = "";
            String nextLink = "";
            Integer currentPage = getCurrentPageNo(currentLink);

            if (currentPage.intValue() > totalPage) {
                currentPage = Integer.valueOf(totalPage);
            }

            if (currentPage.intValue() != 1) {
                preLink = getPreLink(currentLink, currentPage);
                prev = "<a class='prev' target=\"_self\" href=\"" + preLink + "\">上页</a>";
            } else {
                prev = "<span class='prev'>上页</span>";
            }

            if ((totalPage > 5) && (currentPage.intValue() - 3 >= 1)) {
                pageMid.append("<a class='first' target='_self' href='" + getFirstLink(currentLink) + "'>1</a>");
                if (currentPage.intValue() - 3 != 1) {
                    pageMid.append("<span class='dot'>...</span>");
                }

            }

            int pageNoEnd = currentPage.intValue() < 4 ? 5 : currentPage.intValue() + 2;
            int end = pageNoEnd > totalPage ? totalPage : pageNoEnd;
            int pageNoBegin = currentPage.intValue() - (4 - (end - currentPage.intValue()));
            int begin = pageNoBegin < 1 ? 1 : pageNoBegin;

            for (int j = begin; j <= end; j++) {
                if (j == currentPage.intValue())
                    pageMid.append("<span class='current'>" + j + "</span>");
                else {
                    pageMid.append("<a target=\"_self\" href=\"" + getNumLink(currentLink, Integer.valueOf(j)) + "\">" + j + "</a>");
                }
            }

            if ((totalPage > 5) && (currentPage.intValue() + 3 <= totalPage)) {
                if (currentPage.intValue() + 3 != totalPage) {
                    pageMid.append("<span class='dot'>...</span>");
                }
                pageMid.append("<a target='_self' class='last' href='" + getLastLink(currentLink, Integer.valueOf(totalPage)) + "'>" + totalPage + "</a>");
            }

            if (currentPage.intValue() != totalPage) {
                nextLink = getNextLink(currentLink, currentPage);
                next = "<a class='next' target=\"_self\" href=\"" + nextLink + "\">下页</a>";
            } else {
                next = "<span class='next'>下页</span>";
            }

            String countPageNav = "<span class='count'>" + currentPage + "/" + totalPage + "</span>";
            return new String[] { prev, next, pageMid.toString(), countPageNav };
        }
        return new String[] { "", "", "", "" };
    }

    public static Integer getTotalPage(Integer pageSize, Integer count)
    {
        if (count.intValue() < pageSize.intValue()) {
            return Integer.valueOf(1);
        }
        if (count.intValue() % pageSize.intValue() == 0) {
            return Integer.valueOf(count.intValue() / pageSize.intValue());
        }
        return Integer.valueOf(count.intValue() / pageSize.intValue() + 1);
    }

    private static Integer getCurrentPageNo(String currentLink)
    {
        Integer currentPageNo = Integer.valueOf(1);
        String[] urlAry = currentLink.split("[\\?#]");
        if (urlAry.length > 1) {
            String str = urlAry[1];
            String[] paramAry = str.split("&");
            for (String tmp : paramAry) {
                String[] tmpAry = tmp.split("=");
                if ((tmpAry.length > 1) &&
                        (tmpAry[0].equals("pageNo")) && (StringUtil.isNumber(tmpAry[1]))) {
                    currentPageNo = Integer.valueOf(tmpAry[1]);
                }
            }
        }

        return currentPageNo;
    }

    private static String getNumLink(String url, Integer num)
    {
        String numLink = "";
        if (num.intValue() > 0) {
            numLink = getNextLink(url, Integer.valueOf(num.intValue() - 1));
        }
        return numLink;
    }

    private static String getPreLink(String currentLink, Integer currentPageNo)
    {
        if (currentPageNo.intValue() > 1) {
            Integer prePageNo = Integer.valueOf(currentPageNo.intValue() - 1);
            currentLink = formatUrl(currentLink);
            Pattern pattern = Pattern.compile("([^a-zA-Z]pageNo=(\\d+))");
            Matcher matcher = pattern.matcher(currentLink);
            if (matcher.find()) {
                String preLink = currentLink.replaceAll("(pageNo=(\\d+))", "pageNo=" + prePageNo);
                return preLink;
            }
        }
        return currentLink;
    }

    private static String getNextLink(String currentLink, Integer currentPageNo)
    {
        String nextLink = "";
        Integer nextPageNo = Integer.valueOf(currentPageNo.intValue() + 1);
        Pattern pattern = Pattern.compile("([^a-zA-Z]pageNo=(\\d+))");
        currentLink = formatUrl(currentLink);
        Matcher matcher = pattern.matcher(currentLink);
        if (matcher.find()) {
            nextLink = currentLink.replaceAll("(pageNo=(\\d+))", "pageNo=" + nextPageNo);
        } else {
            String[] strAry = currentLink.split("\\?");
            if (strAry.length > 1)
            {
                int maoIndex = currentLink.indexOf("#");
                if (maoIndex >= 0)
                    nextLink = currentLink.substring(0, maoIndex) + "&pageNo=" + nextPageNo + currentLink.substring(maoIndex, currentLink.length());
                else
                    nextLink = currentLink + "&pageNo=" + nextPageNo;
            }
            else
            {
                nextLink = currentLink + "?pageNo=" + nextPageNo;
            }
        }
        return nextLink;
    }

    private static String getFirstLink(String currentLink)
    {
        String firstLink = "";
        Integer firstPageNo = Integer.valueOf(1);
        currentLink = formatUrl(currentLink);
        Pattern pattern = Pattern.compile("([^a-zA-Z]pageNo=(\\d+))");
        Matcher matcher = pattern.matcher(currentLink);
        if (matcher.find()) {
            firstLink = currentLink.replaceAll("(pageNo=(\\d+))", "pageNo=" + firstPageNo);
        } else {
            String[] strAry = currentLink.split("\\?");
            if (strAry.length > 1)
            {
                int maoIndex = currentLink.indexOf("#");
                if (maoIndex >= 0)
                    firstLink = currentLink.substring(0, maoIndex) + "&pageNo=" + firstPageNo + currentLink.substring(maoIndex, currentLink.length());
                else
                    firstLink = currentLink + "&pageNo=" + firstPageNo;
            }
            else
            {
                firstLink = currentLink + "?pageNo=" + firstPageNo;
            }
        }
        return firstLink;
    }

    private static String formatUrl(String currentLink) {
        String[] urlAry = currentLink.split("\\?");
        if (urlAry.length > 1) {
            String str = urlAry[1];
            String[] paramAry = str.split("[&#]");
            for (String tmp : paramAry) {
                String[] tmpAry = tmp.split("=");
                if ((tmpAry.length > 1) &&
                        (tmpAry[0].equals("pageNo")) && (!StringUtil.isNumber(tmpAry[1]))) {
                    currentLink = currentLink.replace("pageNo=" + tmpAry[1], "");
                }
            }
        }

        return currentLink;
    }

    private static String getLastLink(String currentLink, Integer totalPage)
    {
        String lastLink = "";
        currentLink = formatUrl(currentLink);
        Pattern pattern = Pattern.compile("([^a-zA-Z]pageNo=(\\d+))");
        Matcher matcher = pattern.matcher(currentLink);
        if (matcher.find()) {
            lastLink = currentLink.replaceAll("(pageNo=(\\d+))", "pageNo=" + totalPage);
        } else {
            String[] strAry = currentLink.split("\\?");
            if (strAry.length > 1)
            {
                int maoIndex = currentLink.indexOf("#");
                if (maoIndex >= 0)
                    lastLink = currentLink.substring(0, maoIndex) + "&pageNo=" + totalPage + currentLink.substring(maoIndex, currentLink.length());
                else
                    lastLink = currentLink + "&pageNo=" + totalPage;
            }
            else
            {
                lastLink = currentLink + "?pageNo=" + totalPage;
            }
        }
        return lastLink;
    }

    public static void main(String[] args) {
        String link = "http://ts.21cn.com/home/search?keyword=1&pageNo=dd%22%3E%3Cscript%3Eprompt%28940518%29%3C/script%3E";
        Pattern pcListUrlPattern_search = Pattern.compile("/home/search$|home/search/(.*)$");
        Matcher regexpMatcher = pcListUrlPattern_search.matcher(link);
        if (regexpMatcher.find()) {
            System.out.println("==========");
        }
        System.out.println(getNextLink(link, Integer.valueOf(2)));
    }
}